package org.tea.inventory.admin.web.sys;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.domain.sys.SysDictType;
import org.tea.inventory.service.ISysDictTypeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典外层控制器")
@RestController
@RequestMapping("admin/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public Object list(SysDictType dictType,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        PageInfo<SysDictType> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        return ResponseUtil.ok(data);
    }


    /**
     * 查询字典类型详细
     */
    @ApiOperation(value = "查询字典类型详细")
    @GetMapping(value = "/info")
    public Object getInfo(Long dictId) {
        return ResponseUtil.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation(value = "新增字典类型")
    @PostMapping("/add")
    public Object add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ResponseUtil.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return ResponseUtil.ok(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @ApiOperation(value = "修改字典类型")
    @PostMapping("/edit")
    public Object edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ResponseUtil.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return ResponseUtil.ok(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation(value = "删除字典类型")
    @PostMapping("/remove")
    public Object remove(Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ResponseUtil.ok();
    }

    /**
     * 刷新字典缓存
     */
    @ApiOperation(value = "刷新字典缓存")
    @PostMapping("/refreshCache")
    public Object refreshCache() {
        dictTypeService.resetDictCache();
        return ResponseUtil.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation(value = "获取字典选择框列表")
    @GetMapping("/optionselect")
    public Object optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ResponseUtil.ok(dictTypes);
    }
}
